<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2022/3/16
 */

namespace App\Services;


use Illuminate\Support\Facades\Redis;

/**
 * 留言咨询服务
 *
 * Class GuestbookService
 * @package App\Services
 */
class GuestbookService
{
    /**
     * 获取面包屑
     * @return array
     */
    static function getBreadcrumb()
    {
        $href = url('guestbook.html');
        $getGuestbookConfig = ConfigService::getGuestbookConfig();
        return [[
            'title' => $getGuestbookConfig['title'] ?: __('guestbook.guestbook_name'),
            'href_' => " href=${href} ",
            'href_target_' => '_self',
            'rel_' => ""
        ]];
    }


    /**
     * 留言咨询模块seo
     * @return array
     */
    static function meta()
    {
        $siteMeta = MetaService::siteMeta();
        $getGuestbookConfig = ConfigService::getGuestbookConfig();
        $or = trim($getGuestbookConfig['keywords']) || trim($getGuestbookConfig['description']);
        $keywords = $or ? $getGuestbookConfig['keywords'] : $siteMeta['keywords'];
        $description = $or ? $getGuestbookConfig['description'] : $siteMeta['description'];
        return ['title' => ($getGuestbookConfig['title'] ?: __('guestbook.guestbook_name')) . "-" . $siteMeta['title'], 'keywords' => $keywords, 'description' => $description];
    }

    /**
     * 记录留言次数
     * @param string $phone_number
     */
    static function addGuestbookTimesRecord($phone_number = "")
    {
        $phone_number = md5($phone_number);
        Redis::hIncrBy('guestbook_times', $phone_number, 1);
    }

    /**
     * 获取留言次数
     * @param string $phone_number
     * @return int
     */
    static function getGuestbookTimes($phone_number = ""): int
    {
        $phone_number = md5($phone_number);
        $guestbook_times = Redis::hGet('guestbook_times', $phone_number);
        return $guestbook_times ?: 0;
    }

    /**
     * 清空留言次数记录
     * 说明：采用任务调度，每日零点清空记录
     */
    static function clearGuestbookTimesRecord()
    {
        Redis::del('guestbook_times');
    }

}
